#include "oGamePreDefine.h"
#include "oUnitCenter.h"
#include "oGameTypes.h"
#include "Actions/oSwitchAction.h"
#include "Actions/oBrickAction.h"
#include "Actions/oRoleAction.h"
#include "Actions/oPokeAction.h"
#include "oGameAI.h"
#include "oGameScene.h"

int oUnitCenter::loadIndex;
oPlatformWorld* oUnitCenter::world;
oUnitDef* oUnitCenter::unitDefVilly;
oUnitDef* oUnitCenter::unitDefDorothy;
oUnitDef* oUnitCenter::unitDefFlandre;
oUnitDef* oUnitCenter::unitDefBunnyp;
oUnitDef* oUnitCenter::unitDefBunnyg;
oUnitDef* oUnitCenter::unitDefSwitchg;
oUnitDef* oUnitCenter::unitDefSwitch;
oUnitDef* oUnitCenter::unitDefRedBrick1;
oUnitDef* oUnitCenter::unitDefBlueBrick1;
oUnitDef* oUnitCenter::unitDefGreenBrick1;
oUnitDef* oUnitCenter::unitDefGreyBrick1;
oUnitDef* oUnitCenter::unitDefEmptyBrick2;
oUnitDef* oUnitCenter::unitDefGreyBrick4;
oUnitDef* oUnitCenter::unitDefJadeBrick4;

void oUnitCenter::setWorld( oPlatformWorld* world )
{
	oUnitCenter::world = world;
}

void oUnitCenter::loadFlandre()
{
	if (unitDefFlandre)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("flandre.model", CCSize(84, 186), 1.0f, 0.6f, 0.0f);
	unitDef->attackTarget = e_targetMulti;
	unitDef->targetAllow.allow(e_enemy);
	unitDef->targetAllow.allowTerrain(true);
	unitDef->attackBase = 1.0f;
	unitDef->move = 200;
	unitDef->jump = 780;
	unitDef->maxHp = 8;
	unitDef->attackDelay = 20.0f*1.0f/30.0f;
	unitDef->detectDistance = 100;
	unitDef->attackRange = CCSize(260+84/2,200);
	unitDef->attackPower = CCPoint(400,400);
	unitDef->hitEffect = EFFECT_BLOOD;
	unitDef->actions.push_back(oID::ActionIdle);
	unitDef->actions.push_back(oID::ActionWalk);
	unitDef->actions.push_back(oID::ActionTurn);
	unitDef->actions.push_back(oID::ActionMeleeAttack);
	unitDef->actions.push_back(oID::ActionJump);
	unitDef->actions.push_back(oID::ActionStop);
	unitDef->actions.push_back(oID::ActionHit);
	unitDef->actions.push_back(oID::ActionDie);
	unitDefFlandre = unitDef;
	unitDefFlandre->retain();
}
void oUnitCenter::loadDorothy()
{
	if(unitDefDorothy)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("dorothy.model", CCSize(84, 170), 1.0f, 0.4f, 0.0f);
	unitDef->attackTarget = e_targetOne;
	unitDef->targetAllow.allow(e_enemy);
	unitDef->targetAllow.allowTerrain(true);
	unitDef->attackBase = 1.0f;
	unitDef->move = 150;
	unitDef->jump = 600;
	unitDef->maxHp = 8;
	unitDef->attackDelay = 18.0f*1.0f/30.0f;
	unitDef->detectDistance = 100;
	unitDef->attackRange = CCSize(500+84/2,100);
	unitDef->attackPower = CCPoint(300,300);
	unitDef->hitEffect = EFFECT_BLOOD;
	unitDef->getModelDef()->addKeyPoint(CCPoint(-66, 20));
	oBulletDef* bulletDef = oBulletDef::create();
	bulletDef->setAsCircle(10);
	bulletDef->setVelocity(15, 1200);
	bulletDef->setGravityScale(1.0f);
	bulletDef->lifeTime = 1.0f;
	oFace* face = oFace::create("arrow.png", CCPointZero);
	bulletDef->setFace(face);
	unitDef->setBulletDef(bulletDef);
	unitDef->actions.push_back(oID::ActionIdle);
	unitDef->actions.push_back(oID::ActionWalk);
	unitDef->actions.push_back(oID::ActionTurn);
	unitDef->actions.push_back(oID::ActionRangeAttack);
	unitDef->actions.push_back(oID::ActionJump);
	unitDef->actions.push_back(oID::ActionStop);
	unitDef->actions.push_back(oID::ActionHit);
	unitDef->actions.push_back(oID::ActionDie);
	unitDefDorothy = unitDef;
	unitDefDorothy->retain();
}
void oUnitCenter::loadVilly()
{
	if(unitDefVilly)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("villy.model", CCSize(84, 186), 1.0f, 0.4f, 0.0f);
	unitDef->attackTarget = e_targetOne;
	unitDef->targetAllow.allow(e_enemy);
	unitDef->targetAllow.allowTerrain(true);
	unitDef->attackBase = 1.0f;
	unitDef->move = 240;
	unitDef->jump = 600;
	unitDef->maxHp = 8;
	unitDef->detectDistance = 100;
	unitDef->attackRange = CCSize(300+84/2,100);
	unitDef->attackPower = CCPoint(200,300);
	unitDef->hitEffect = EFFECT_BLOOD;
	unitDef->getModelDef()->addKeyPoint(CCPoint(-110,-10));
	oBulletDef* bulletDef = oBulletDef::create();
	bulletDef->setAsCircle(15);
	bulletDef->setVelocity(0, 400);
	bulletDef->setGravityScale(-0.4f);
	bulletDef->lifeTime = 3.0f;
	oFace* face = oFace::create("misc.clip|4", CCPointZero);
	bulletDef->setFace(face);
	unitDef->setBulletDef(bulletDef);
	unitDef->actions.push_back(oID::ActionIdle);
	unitDef->actions.push_back(oID::ActionWalk);
	unitDef->actions.push_back(oID::ActionTurn);
	unitDef->actions.push_back(oID::ActionJump);
	unitDef->actions.push_back(oID::ActionStop);
	unitDef->actions.push_back(oID::ActionHit);
	unitDef->actions.push_back(oID::ActionDie);
	unitDefVilly = unitDef;
	unitDefVilly->retain();
}

void oUnitCenter::loadBunnyp()
{
	if(unitDefBunnyp)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("bunnyp.model",CCSize(132, 128), 1.0f, 0.4f, 0.0f);
	unitDef->attackTarget = e_targetOne;
	unitDef->targetAllow.allow(e_enemy);
	unitDef->targetAllow.allowTerrain(true);
	unitDef->attackBase = 1.0f;
	unitDef->move = 150;
	unitDef->jump = 600;
	unitDef->maxHp = 3;
	unitDef->detectDistance = 100;
	unitDef->attackRange = CCSize(60+132/2,80);
	unitDef->attackPower = CCPoint(400,400);
	unitDef->hitEffect = EFFECT_BLOOD;
	unitDef->actions.push_back(oID::ActionIdle);
	unitDef->actions.push_back(oID::ActionWalk);
	unitDef->actions.push_back(oID::ActionTurn);
	unitDef->actions.push_back(oID::ActionMeleeAttack);
	unitDef->actions.push_back(oID::ActionJump);
	unitDef->actions.push_back(oID::ActionStop);
	unitDef->actions.push_back(oID::ActionHit);
	unitDef->actions.push_back(oID::ActionDie);
	unitDefBunnyp = unitDef;
	unitDefBunnyp->retain();
}
void oUnitCenter::loadBunnyg()
{
	if(unitDefBunnyg)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("bunnyg.model", CCSize(132, 128), 1.0f, 0.4f, 0.0f);
	unitDef->attackTarget = e_targetOne;
	unitDef->targetAllow.allow(e_enemy);
	unitDef->targetAllow.allowTerrain(true);
	unitDef->attackBase = 1.0f;
	unitDef->move = 150;
	unitDef->jump = 600;
	unitDef->maxHp = 3;
	unitDef->detectDistance = 100;
	unitDef->attackRange = CCSize(60+132/2,100);
	unitDef->attackPower = CCPoint(400,400);
	unitDef->hitEffect = EFFECT_BLOOD;
	unitDef->actions.push_back(oID::ActionIdle);
	unitDef->actions.push_back(oID::ActionWalk);
	unitDef->actions.push_back(oID::ActionTurn);
	unitDef->actions.push_back(oID::ActionMeleeAttack);
	unitDef->actions.push_back(oID::ActionJump);
	unitDef->actions.push_back(oID::ActionStop);
	unitDef->actions.push_back(oID::ActionHit);
	unitDef->actions.push_back(oID::ActionDie);
	unitDefBunnyg = unitDef;
	unitDefBunnyg->retain();
}

void oUnitCenter::loadSwitch()
{
	if(unitDefSwitch)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("switch.model", CCSizeZero);
	unitDef->attackRange = CCSize(80,126);
	unitDef->setIsStatic(true);
	unitDefSwitch = unitDef;
	unitDefSwitch->retain();
}
void oUnitCenter::loadSwitchg()
{
	if(unitDefSwitchg)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("switch0.model", CCSizeZero);
	unitDef->attackRange = CCSize(80,126);
	unitDef->setIsStatic(true);
	unitDefSwitchg = unitDef;
	unitDefSwitchg->retain();
}
void oUnitCenter::loadRedBrick1()
{
	if(unitDefRedBrick1)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("redbrick1.model", CCSize(0, 0));
	unitDef->getBodyDef()->attachPolygon(90, 90, 1.0f, 0.4f, 0.0f);
	unitDef->maxHp = 2;
	unitDef->hitEffect = EFFECT_HEART;
	unitDefRedBrick1 = unitDef;
	unitDefRedBrick1->retain();
}
void oUnitCenter::loadBlueBrick1()
{
	if(unitDefBlueBrick1)
	{
		return;
	}	
	oUnitDef* unitDef = oUnitDef::create("bluebrick1.model", CCSize(0, 0));
	unitDef->getBodyDef()->attachPolygon(90, 90, 1.0f, 0.4f, 0.0f);
	unitDef->maxHp = 2;
	unitDef->hitEffect = EFFECT_HEART;
	unitDefBlueBrick1 = unitDef;
	unitDefBlueBrick1->retain();
}
void oUnitCenter::loadGreenBrick1()
{
	if(unitDefGreenBrick1)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("greenbrick1.model", CCSize(0, 0));
	unitDef->getBodyDef()->attachPolygon(90, 90, 1.0f, 0.4f, 0.0f);
	unitDef->maxHp = 2;
	unitDef->hitEffect = EFFECT_HEART;
	unitDefGreenBrick1 = unitDef;
	unitDefGreenBrick1->retain();
}
void oUnitCenter::loadGreyBrick1()
{
	if(unitDefGreyBrick1)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("greybrick1.model", CCSize(0, 0));
	unitDef->getBodyDef()->attachPolygon(90, 90, 1.0f, 0.4f, 0.0f);
	unitDef->maxHp = 2;
	unitDef->hitEffect = EFFECT_HEART;
	unitDefGreyBrick1 = unitDef;
	unitDefGreyBrick1->retain();
}
void oUnitCenter::loadEmptyBrick2()
{
	if(unitDefEmptyBrick2)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("emptybrick2.model", CCSize(0, 0));
	unitDef->getBodyDef()->attachPolygon(90, 210, 0.5f, 0.4f, 0.0f);
	unitDef->maxHp = 3;
	unitDef->hitEffect = EFFECT_HEART;
	unitDefEmptyBrick2 = unitDef;
	unitDefEmptyBrick2->retain();
}
void oUnitCenter::loadGreyBrick4()
{
	if(unitDefGreyBrick4)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("greybrick4.model", CCSize(0, 0));
	unitDef->getBodyDef()->attachPolygon(450, 90, 0.5f, 0.4f, 0.0f);
	unitDef->maxHp = 4;
	unitDef->hitEffect = EFFECT_HEART;
	unitDefGreyBrick4 = unitDef;
	unitDefGreyBrick4->retain();
}
void oUnitCenter::loadJadeBrick4()
{
	if(unitDefJadeBrick4)
	{
		return;
	}
	oUnitDef* unitDef = oUnitDef::create("jadebrick4.model", CCSize(0, 0));
	unitDef->getBodyDef()->attachPolygon(450, 90, 1.0f, 0.4f, 0.0f);
	unitDef->maxHp = 4;
	unitDef->hitEffect = EFFECT_HEART;
	unitDefJadeBrick4 = unitDef;
	unitDefJadeBrick4->retain();
}
void oUnitCenter::loadPoke()
{
	CCTextureCache::sharedTextureCache()->addImage("poke.png");
	oSharedClipCache.load("poke.clip");
	CCTextureCache::sharedTextureCache()->addImage("poke_enemy.png");
	oSharedClipCache.load("poke_enemy.clip");
}

typedef void (*LoadFunc)();
static LoadFunc loadFuncs[] =
{
	oUnitCenter::loadFlandre,
	oUnitCenter::loadDorothy,
	oUnitCenter::loadVilly,
	oUnitCenter::loadBunnyp,
	oUnitCenter::loadBunnyg,
	oUnitCenter::loadSwitch,
	oUnitCenter::loadSwitchg,
	oUnitCenter::loadRedBrick1,
	oUnitCenter::loadBlueBrick1,
	oUnitCenter::loadGreenBrick1,
	oUnitCenter::loadGreyBrick1,
	oUnitCenter::loadEmptyBrick2,
	oUnitCenter::loadGreyBrick4,
	oUnitCenter::loadJadeBrick4,
	oUnitCenter::loadPoke,
	oUnitCenter::loadSound0,
	oUnitCenter::loadSound1,
	oUnitCenter::loadSound2,
	oUnitCenter::loadSound3,
	oUnitCenter::loadSound4,
	oUnitCenter::loadSound5,
	oUnitCenter::loadSound6,
	oUnitCenter::loadSound7,
	oUnitCenter::loadSound8,
	oUnitCenter::loadSound9,
	oUnitCenter::loadSound10,
	oUnitCenter::loadSound11,
	oUnitCenter::loadSound12,
	oUnitCenter::loadSound13,
	oUnitCenter::loadSound14,
	oUnitCenter::loadSound15
};
int oUnitCenter::loadCount = sizeof(loadFuncs)/sizeof(LoadFunc);

bool oUnitCenter::load()
{
	if (loadIndex < loadCount)
	{
		loadFuncs[loadIndex]();
		loadIndex++;
		return false;
	}
	return true;
}
void oUnitCenter::unload()
{
	loadIndex = 0;
	CC_SAFE_RELEASE_NULL(unitDefFlandre);
	CC_SAFE_RELEASE_NULL(unitDefDorothy);
	CC_SAFE_RELEASE_NULL(unitDefVilly);
	CC_SAFE_RELEASE_NULL(unitDefBunnyp);
	CC_SAFE_RELEASE_NULL(unitDefBunnyg);
	CC_SAFE_RELEASE_NULL(unitDefSwitch);
	CC_SAFE_RELEASE_NULL(unitDefSwitchg);
	CC_SAFE_RELEASE_NULL(unitDefRedBrick1);
	CC_SAFE_RELEASE_NULL(unitDefBlueBrick1);
	CC_SAFE_RELEASE_NULL(unitDefGreenBrick1);
	CC_SAFE_RELEASE_NULL(unitDefGreyBrick1);
	CC_SAFE_RELEASE_NULL(unitDefEmptyBrick2);
	CC_SAFE_RELEASE_NULL(unitDefGreyBrick4);
	CC_SAFE_RELEASE_NULL(unitDefJadeBrick4);
}

oBody* oUnitCenter::createTerrainRect( float width, float height, float x, float y )
{
	oBodyDef* bodyDef = oBodyDef::create();
	bodyDef->type = b2_staticBody;
	bodyDef->attachPolygon(width, height, 0.0f, 0.6f, 0.0f);
	oBody* body = bodyDef->toBody(world, GROUP_TERRAIN, x, y, 0);
	world->addChild(body, LAYER_BACKGROUND_ID);
	return body;
}

void onHeroDie(oAction* action)
{
	oUnit* hero = action->getOwner();
	if (hero->getGroup() == GROUP_HIDE_ME)
	{
		oGameScene::createSwitcher(true);
		oGameScene::setMana(GROUP_ME, MAX_MANA);
		oGameScene::addMana(GROUP_ENEMY, 1);
		hero->getB2Body()->SetGravityScale(0.0f);
		hero->runAction(
			CCSequence::actions(
			CCEaseSineInOut::actionWithAction(
			CCMoveTo::actionWithDuration(10.0f, CCPoint(512, hero->getPositionY()))),
			CCCallFuncND::actionWithTarget(oGameScene::instance(),callfuncND_selector(oGameScene::onRiseHero), (void*)hero),
			NULL));
		SimpleAudioEngine::sharedEngine()->playEffect("sound/hero_fall.wav");
	}
	else if (hero->getGroup() == GROUP_HIDE_ENEMY)
	{
		oGameScene::createSwitcher(false);
		oGameScene::setMana(GROUP_ENEMY, MAX_MANA);
		oGameScene::addMana(GROUP_ME, 1);
		hero->getB2Body()->SetGravityScale(0.0f);
		hero->runAction(
			CCSequence::actions(
			CCEaseSineInOut::actionWithAction(
			CCMoveTo::actionWithDuration(10.0f, CCPoint(3584, hero->getPositionY()))),
			CCCallFuncND::actionWithTarget(oGameScene::instance(),callfuncND_selector(oGameScene::onRiseHero), (void*)hero),
			NULL));
		SimpleAudioEngine::sharedEngine()->playEffect("sound/hero_kill.wav");
	}
}
oUnit* oUnitCenter::createFlandre(bool isMe, float x, float y )
{
	oUnit* unit = unitDefFlandre->toUnit(world, isMe ? GROUP_ME : GROUP_ENEMY, x, y, isMe);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_HERO);
	unit->attachAction(new oRoleSwitch(unit));
	unit->attachAction(new oRoleStrike(unit));
	unit->instincts.add(oSharedGameAI.damegeReaction);
	unit->attackSpeed = 1.5f;
	unit->moveSpeed = 1.5f;
	unit->getAction(oID::ActionDie)->actionEnd += onHeroDie;
	unit->setReflexArc(oSharedGameAI.heroFight);
	world->addChild(unit, isMe ? LAYER_HERO_ME_ID : LAYER_HERO_ENEMY_ID);
// 	CCSprite* seu = CCSprite::spriteWithFile("SEU.png");
// 	seu->setPositionY(150.0f);
// 	unit->addChild(seu);
	return unit;
}
oUnit* oUnitCenter::createDorothy( bool isMe, float x, float y )
{
	oUnit* unit = unitDefDorothy->toUnit(world, isMe ? GROUP_ME : GROUP_ENEMY, x, y, isMe);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_HERO);
	unit->attachAction(new oRoleSwitch(unit));
	unit->attachAction(new oRoleStrike(unit));
	unit->instincts.add(oSharedGameAI.damegeReaction);
	unit->attackSpeed = 1.5f;
	unit->moveSpeed = 1.5f;
	unit->getAction(oID::ActionDie)->actionEnd += onHeroDie;
	unit->setReflexArc(oSharedGameAI.heroFight);
	world->addChild(unit, isMe ? LAYER_HERO_ME_ID : LAYER_HERO_ENEMY_ID);
	return unit;
}
oUnit* oUnitCenter::createVilly( bool isMe, float x, float y )
{
	oUnit* unit = unitDefVilly->toUnit(world, isMe ? GROUP_ME : GROUP_ENEMY, x, y, isMe);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_HERO);
	unit->attachAction(new oVillyAttack(unit));
	unit->attachAction(new oRoleSwitch(unit));
	unit->attachAction(new oRoleStrike(unit));
	unit->instincts.add(oSharedGameAI.damegeReaction);
	unit->attackSpeed = 1.5f;
	unit->moveSpeed = 1.5f;
	unit->getAction(oID::ActionDie)->actionEnd += onHeroDie;
	unit->setReflexArc(oSharedGameAI.heroFight);
	world->addChild(unit, isMe ? LAYER_HERO_ME_ID : LAYER_HERO_ENEMY_ID);
	return unit;
}

void onBunnyDie(oAction* action)
{
	oUnit* bunny = action->getOwner();
	if (bunny->getGroup() == GROUP_HIDE_ME)
	{
		oGameScene::bunnyMeCount--;
		oGameScene::addMana(GROUP_ENEMY, 1);
	}
	else if (bunny->getGroup() == GROUP_HIDE_ENEMY)
	{
		oGameScene::bunnyEnemyCount--;
		oGameScene::addMana(GROUP_ME, 1);
	}
	bunny->destroy();
}
oUnit* oUnitCenter::createBunnyp(bool isMe, float x, float y )
{
	oUnit* unit = unitDefBunnyp->toUnit(world, isMe ? GROUP_ME : GROUP_ENEMY, x, y, isMe);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BUNNY);
	unit->attachAction(new oRoleStrike(unit));
	unit->instincts.add(oSharedGameAI.damegeReaction);
	//unit->attackSpeed = 1.5f;
	unit->moveSpeed = 1.5f;
	unit->sensity = 0.5f;
	unit->getAction(oID::ActionDie)->actionEnd += onBunnyDie;
	unit->setReflexArc(oSharedGameAI.getRandomBunnyAI());
	world->addChild(unit, LAYER_BUNNY_ID);
	return unit;
}
oUnit* oUnitCenter::createBunnyg( bool isMe, float x, float y )
{
	oUnit* unit = unitDefBunnyg->toUnit(world, isMe ? GROUP_ME : GROUP_ENEMY, x, y, isMe);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BUNNY);
	unit->attachAction(new oRoleStrike(unit));
	unit->instincts.add(oSharedGameAI.damegeReaction);
	//unit->attackSpeed = 1.5f;
	unit->moveSpeed = 1.5f;
	unit->sensity = 0.5f;
	unit->getAction(oID::ActionDie)->actionEnd += onBunnyDie;
	unit->setReflexArc(oSharedGameAI.getRandomBunnyAI());
	world->addChild(unit, LAYER_BUNNY_ID);
	return unit;
}
void onSwitherHit(oAction* action)
{
	oUnit* owner = action->getOwner();
	int id = (int)(owner->properties[oStr::SwitcherID]);
	if (owner->getGroup() == GROUP_ME)
	{
		oGameScene::switcherMe[id] = nullptr;
	}
	else
	{
		oGameScene::switcherEnemy[id] = nullptr;
	}
	action->actionEnd -= onSwitherHit;
	owner->getAction(ACTION_ROLE_STRIKE)->actionEnd -= onSwitherHit;
	action->getOwner()->setReflexArc(oSharedGameAI.bunnySearchAttack);
}
oUnit* oUnitCenter::createBunnySwitcher( bool isMe, int id, float x, float y )
{
	oUnit* unit = (isMe ? unitDefBunnyg : unitDefBunnyp)->toUnit(world, isMe ? GROUP_ME : GROUP_ENEMY, x, y, isMe);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BUNNY);
	unit->attachAction(new oRoleStrike(unit));
	unit->attachAction(new oRoleSwitch(unit));
	unit->instincts.add(oSharedGameAI.damegeReaction);
	unit->getAction(oID::ActionHit)->actionEnd += onSwitherHit;
	unit->getAction(ACTION_ROLE_STRIKE)->actionEnd += onSwitherHit;
	unit->getAction(oID::ActionDie)->actionEnd += onBunnyDie;
	//unit->attackSpeed = 1.5f;
	unit->moveSpeed = 1.5f;
	unit->properties[oStr::SwitcherID] = (float)id;
	unit->setReflexArc(oSharedGameAI.bunnySwitcher);
	world->addChild(unit, LAYER_BUNNY_ID);
	return unit;
}
oUnit* oUnitCenter::createBunnyFighter( bool isMe, float x, float y )
{
	if (isMe)
	{
		return oUnitCenter::createBunnyg(true, x, y);
	}
	else
	{
		return oUnitCenter::createBunnyp(false, x, y);
	}
}

void onMeSwitchPushed(oAction* action)
{
	oUnit* bunny = oUnitCenter::createBunnyFighter(true, 1000, 1004-500);
	if (oGameScene::bunnyMeCount < MAX_BUNNY)
	{
		oGameScene::bunnyMeCount++;
	}
	else
	{
		bunny->doIt(oID::ActionDie);
	}
}
void onMeSwitchgPushed(oAction* action)
{
	oUnitCenter::createPoke(true, 192, 1004-512);
}
void onEnemySwitchPushed(oAction* action)
{
	oUnit* bunny = oUnitCenter::createBunnyFighter(false, 3130, 1004-500);
	if (oGameScene::bunnyEnemyCount < MAX_BUNNY)
	{
		oGameScene::bunnyEnemyCount++;
	}
	else
	{
		bunny->doIt(oID::ActionDie);
	}
}
void onEnemySwitchgPushed(oAction* action)
{
	oUnitCenter::createPoke(false, 3904, 1004-512);
}
void onUnitLeaveSwitchArea(oSensor* sensor, oBody* body)
{
	if (oSharedData.isPlayerUnit(body))
	{
		oUnit* unit = (oUnit*)body;
		if (unit->getTag() == TAG_BUNNY || unit->getTag() == TAG_HERO)
		{
			oUnit* pSwitch = (oUnit*)sensor->getOwner();
			if (oSharedData.getRelation(unit, pSwitch) == e_friend)
			{
				unit->properties[oStr::IsAtSwitch] = 0;
			}
		}
	}
}
oUnit* oUnitCenter::createSwitch( bool isMe, float x, float y )
{
	oUnit* unit = unitDefSwitch->toUnit(world, isMe ? GROUP_ME : GROUP_ENEMY, x, y, isMe);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_SWITCH);
	unit->attachAction(new oSwitchIdle(unit));
	oAction* switchPush = new oSwitchPush(unit);
	switchPush->actionEnd = (isMe ? onMeSwitchPushed : onEnemySwitchPushed);
	unit->attachAction(switchPush);
	unit->doIt(ACTION_SWITCH_IDLE);
	oSensor* sensor = unit->getAttackSensor();
	sensor->bodyLeave = onUnitLeaveSwitchArea;
	world->addChild(unit, LAYER_SWITCH_ID);
	return unit;
}
oUnit* oUnitCenter::createSwitchg( bool isMe, float x, float y )
{
	oUnit* unit = unitDefSwitchg->toUnit(world, isMe ? GROUP_ME : GROUP_ENEMY, x, y, isMe);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_SWITCHG);
	unit->attachAction(new oSwitchIdle(unit));
	oAction* switchPush = new oSwitchPush(unit);
	switchPush->actionEnd = (isMe ? onMeSwitchgPushed : onEnemySwitchgPushed);
	unit->attachAction(switchPush);
	unit->doIt(ACTION_SWITCH_IDLE);
	oSensor* sensor = unit->getAttackSensor();
	sensor->bodyLeave = onUnitLeaveSwitchArea;
	world->addChild(unit, LAYER_SWITCH_ID);
	return unit;
}

void onBrickDie(oAction* action)
{
	oUnit* brick = action->getOwner();
	if (brick->getGroup() == GROUP_BLOCK_ME)
	{
		oGameScene::isMeAttacked = true;
		oGameScene::brickMeCount--;
		if (oGameScene::brickMeCount <= 0)
		{
			oGameScene::win(GROUP_ENEMY);
		}
		oGameScene::addMana(GROUP_ENEMY, 1);
	}
	else if (brick->getGroup() == GROUP_BLOCK_ENEMY)
	{
		oGameScene::isEnemyAttacked = true;
		oGameScene::brickEnemyCount--;
		if (oGameScene::brickEnemyCount <= 0)
		{
			oGameScene::win(GROUP_ME);
		}
		oGameScene::addMana(GROUP_ME, 1);
	}
	brick->destroy();
}
oUnit* oUnitCenter::createRedBrick1( bool isMe, float x, float y )
{
	oUnit* unit = unitDefRedBrick1->toUnit(world, isMe ? GROUP_BLOCK_ME : GROUP_BLOCK_ENEMY, x, y);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BRICK);
	unit->getModel()->setLook(oNumberStr[(int)unitDefRedBrick1->maxHp-1]);
	unit->attachAction(new oBrickHit(unit));
	oBrickDie* die = new oBrickDie(unit);
	die->actionEnd += onBrickDie;
	unit->attachAction(die);
	world->addChild(unit, LAYER_BRICK_ID);
	return unit;
}
oUnit* oUnitCenter::createBlueBrick1( bool isMe, float x, float y )
{
	oUnit* unit = unitDefBlueBrick1->toUnit(world, isMe ? GROUP_BLOCK_ME : GROUP_BLOCK_ENEMY, x, y);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BRICK);
	unit->getModel()->setLook(oNumberStr[(int)unitDefBlueBrick1->maxHp-1]);
	unit->attachAction(new oBrickHit(unit));
	oBrickDie* die = new oBrickDie(unit);
	die->actionEnd += onBrickDie;
	unit->attachAction(die);
	world->addChild(unit, LAYER_BRICK_ID);
	return unit;
}
oUnit* oUnitCenter::createGreenBrick1( bool isMe, float x, float y )
{
	oUnit* unit = unitDefGreenBrick1->toUnit(world, isMe ? GROUP_BLOCK_ME : GROUP_BLOCK_ENEMY, x, y);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BRICK);
	unit->getModel()->setLook(oNumberStr[(int)unitDefGreenBrick1->maxHp-1]);
	unit->attachAction(new oBrickHit(unit));
	oBrickDie* die = new oBrickDie(unit);
	die->actionEnd += onBrickDie;
	unit->attachAction(die);
	world->addChild(unit, LAYER_BRICK_ID);
	return unit;
}
oUnit* oUnitCenter::createGreyBrick1( bool isMe, float x, float y )
{
	oUnit* unit = unitDefGreyBrick1->toUnit(world, isMe ? GROUP_BLOCK_ME : GROUP_BLOCK_ENEMY, x, y);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BRICK);
	unit->getModel()->setLook(oNumberStr[(int)unitDefGreyBrick1->maxHp-1]);
	unit->attachAction(new oBrickHit(unit));
	oBrickDie* die = new oBrickDie(unit);
	die->actionEnd += onBrickDie;
	unit->attachAction(die);
	world->addChild(unit, LAYER_BRICK_ID);
	return unit;
}
oUnit* oUnitCenter::createEmptyBrick2( bool isMe, float x, float y )
{
	oUnit* unit = unitDefEmptyBrick2->toUnit(world, isMe ? GROUP_BLOCK_ME : GROUP_BLOCK_ENEMY, x, y);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BRICK);
	unit->getModel()->setLook(oNumberStr[(int)unitDefEmptyBrick2->maxHp-1]);
	unit->attachAction(new oBrickHit(unit));
	oBrickDie* die = new oBrickDie(unit);
	die->actionEnd += onBrickDie;
	unit->attachAction(die);
	world->addChild(unit, LAYER_BRICK_ID);
	return unit;
}
oUnit* oUnitCenter::createGreyBrick4( bool isMe, float x, float y )
{
	oUnit* unit = unitDefGreyBrick4->toUnit(world, isMe ? GROUP_BLOCK_ME : GROUP_BLOCK_ENEMY, x, y);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BRICK);
	unit->getModel()->setLook(oNumberStr[(int)unitDefGreyBrick4->maxHp-1]);
	unit->attachAction(new oBrickHit(unit));
	oBrickDie* die = new oBrickDie(unit);
	die->actionEnd += onBrickDie;
	unit->attachAction(die);
	world->addChild(unit, LAYER_BRICK_ID);
	return unit;
}
oUnit* oUnitCenter::createJadeBrick4( bool isMe, float x, float y )
{
	oUnit* unit = unitDefJadeBrick4->toUnit(world, isMe ? GROUP_BLOCK_ME : GROUP_BLOCK_ENEMY, x, y);
	unit->getModel()->setScale(2.0f);
	unit->setTag(TAG_BRICK);
	unit->getModel()->setLook(oNumberStr[(int)unitDefJadeBrick4->maxHp-1]);
	unit->attachAction(new oBrickHit(unit));
	oBrickDie* die = new oBrickDie(unit);
	die->actionEnd += onBrickDie;
	unit->attachAction(die);
	world->addChild(unit, LAYER_BRICK_ID);
	return unit;
}

void onPokeStrikeUnit(oSensor* sensor, oBody* body)
{
	if (oSharedData.isPlayerUnit(body))
	{
		oUnit* unit = (oUnit*)body;
		if (unit->getTag() == TAG_BUNNY || unit->getTag() == TAG_HERO)
		{
			oUnit* poke = (oUnit*)sensor->getOwner();
			if (oSharedData.getRelation(unit, poke) == e_enemy)
			{
				if (poke->getVelocityX() > 0.0f)
				{
					if (poke->getPositionX() < unit->getPositionX())
					{
						unit->setVelocity(500.0f, 400.0f);
						unit->doIt(ACTION_ROLE_STRIKE);
					}
				}
				else if (poke->getPositionX() >= unit->getPositionX())
				{
					unit->setVelocity(-500.0f, 400.0f);
					unit->doIt(ACTION_ROLE_STRIKE);
				}
			}
		}
	}
}
oUnit* oUnitCenter::createPoke( bool isMe, float x, float y )
{
	oUnitDef* unitDef = oUnitDef::create("", CCSizeZero);
	unitDef->getBodyDef()->attachCircle(192, 10.0f, 0.1f, 0.4f);
	unitDef->detectDistance = 194;
	oUnit* unit = unitDef->toUnit(world, isMe ? GROUP_POKE_ME : GROUP_POKE_ENEMY, x, y);
	unit->setTag(TAG_POKE);
	if (isMe)
	{
		CCSprite* sprite = (CCSprite*)oFace::create("poke.clip|1", CCPointZero)->toNode();
		sprite->getTexture()->setAliasTexParameters();
		unit->addChild(sprite);
		sprite->setScale(4.0f);
	}
	else
	{
		CCSprite* sprite = (CCSprite*)oFace::create("poke_enemy.clip|1", CCPointZero)->toNode();
		sprite->getTexture()->setAliasTexParameters();
		unit->addChild(sprite);
		sprite->setScale(4.0f);
	}
	unit->setVelocityX(unit->getGroup() == GROUP_POKE_ME ? 400.0f : -400.0f);
	oSensor* sensor = unit->getDetectSensor();
	sensor->bodyEnter += onPokeStrikeUnit;
	unit->attachAction(new oPokeAction(unit));
	unit->doIt(ACTION_POKE);
	world->addChild(unit, 2);
	SimpleAudioEngine::sharedEngine()->playEffect("sound/quake.wav");
	return unit;
}

oBody* oUnitCenter::createRect(const char* clipname, float x, float y )
{
	CCSprite* sprite = (CCSprite*)oFace::create(clipname, CCPointZero)->toNode();
	sprite->setScale(2.2f);
	CCSize size = sprite->getContentSize();
	oBodyDef* bodyDef = oBodyDef::create();
	bodyDef->attachPolygon(size.width*2.2f, size.height*2.2f ,5.0f, 0.0f, 1.0f);
	oBody* body = bodyDef->toBody(world, GROUP_RECT, x, y, 0);
	body->addChild(sprite);
	world->addChild(body, LAYER_README_ID);
	return body;
}

oBody* oUnitCenter::createBoarder( float width, float height, float x, float y )
{
	oBodyDef* bodyDef = oBodyDef::create();
	bodyDef->type = b2_staticBody;
	bodyDef->attachPolygon(width, height,0.0f, 0.6f, 0.0f);
	oBody* body = bodyDef->toBody(world, GROUP_RECT, x, y, 0);
	world->addChild(body, LAYER_README_ID);
	return body;
}

int oUnitCenter::totalLoad()
{
	return loadCount;
}

int oUnitCenter::currentLoad()
{
	return loadIndex;
}

void oUnitCenter::loadSound0()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/b_att.wav");
}

void oUnitCenter::loadSound1()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/choose.wav");
}

void oUnitCenter::loadSound2()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/d_att.wav");
}

void oUnitCenter::loadSound3()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/di.wav");
}

void oUnitCenter::loadSound4()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/explosion.wav");
}

void oUnitCenter::loadSound5()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/f_att.wav");
}

void oUnitCenter::loadSound6()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/hero_fall.wav");
}
void oUnitCenter::loadSound7()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/hero_kill.wav");
}
void oUnitCenter::loadSound8()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/hero_lose.wav");
}
void oUnitCenter::loadSound9()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/hero_quit.wav");
}
void oUnitCenter::loadSound10()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/hit.wav");
}
void oUnitCenter::loadSound11()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/select.wav");
}
void oUnitCenter::loadSound12()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/switch.wav");
}
void oUnitCenter::loadSound13()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/v_att.wav");
}
void oUnitCenter::loadSound14()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/quake.wav");
}
void oUnitCenter::loadSound15()
{
	SimpleAudioEngine::sharedEngine()->preloadEffect("sound/hero_win.wav");
}